package com.blocks.gui;

import com.blocks.ScriptBlocksApplication;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.CheckBox;
import de.lessvoid.nifty.controls.Label;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;

public class LevelScreenController implements ScreenController {
	private Nifty nifty;
	private Screen screen;
	private Label label;
	private CheckBox shadowCheckBox;
	private CheckBox lockToGridCheckBox;
	private CheckBox moveBlockXCheckBox;
	private CheckBox moveBlockYCheckBox;
	private CheckBox moveBlockZCheckBox;
	private CheckBox skyMapCheckBox;
	private CheckBox showStatsCheckBox;
	private TextField textField;

	
	@Override
	public void bind(Nifty nifty, Screen screen) {
		this.nifty = nifty;
		this.screen = screen;
		this.label = screen.findNiftyControl("label1", Label.class);
		this.shadowCheckBox = screen.findNiftyControl("shadowCheckBox", CheckBox.class);
		this.skyMapCheckBox = screen.findNiftyControl("skyMapCheckBox", CheckBox.class);
		this.lockToGridCheckBox = screen.findNiftyControl("lockToGridCheckBox", CheckBox.class);
		this.moveBlockXCheckBox = screen.findNiftyControl("moveBlockXCheckBox", CheckBox.class);
		this.moveBlockYCheckBox = screen.findNiftyControl("moveBlockYCheckBox", CheckBox.class);
		this.moveBlockZCheckBox = screen.findNiftyControl("moveBlockZCheckBox", CheckBox.class);
		this.showStatsCheckBox = screen.findNiftyControl("showStatsCheckBox", CheckBox.class);
		this.textField = screen.findNiftyControl("nextLevelFileName", TextField.class);
		
		this.label.setText(ScriptBlocksApplication.getInstance().getLevelProperties().getLevelDescription());
		this.shadowCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isShowShadow());
		this.skyMapCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isSkyMap());
		this.lockToGridCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isLockMasterToGrid());
		this.moveBlockXCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isMoveBlockX());
		this.moveBlockYCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isMoveBlockY());
		this.moveBlockZCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isMoveBlockZ());
		this.showStatsCheckBox.setChecked(ScriptBlocksApplication.getInstance().getLevelProperties().isShowStats());
		this.textField.setText(ScriptBlocksApplication.getInstance().getLevelProperties().getNextLevelFileName());
	}

	@Override
	public void onEndScreen() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStartScreen() {
		// TODO Auto-generated method stub

	}
	
	public void backToMainMenu() {
		if(ScriptBlocksApplication.getInstance().getLevelProperties().isShowShadow()!=this.shadowCheckBox.isChecked()){
			ScriptBlocksApplication.getInstance().getLevelProperties().setShowShadow(this.shadowCheckBox.isChecked());
			if(this.shadowCheckBox.isChecked()){
				ScriptBlocksApplication.getInstance().addShadow();
			}else {
				ScriptBlocksApplication.getInstance().removeShadow();
			}
		}
		if(ScriptBlocksApplication.getInstance().getLevelProperties().isSkyMap()!=this.skyMapCheckBox.isChecked()){
			ScriptBlocksApplication.getInstance().getLevelProperties().setSkyMap(this.skyMapCheckBox.isChecked());
			if(this.skyMapCheckBox.isChecked()){
				ScriptBlocksApplication.getInstance().addSkyMap();
			}else {
				ScriptBlocksApplication.getInstance().removeSkyMap();
			}
		}
		
		if(ScriptBlocksApplication.getInstance().getLevelProperties().isLockMasterToGrid()!=this.lockToGridCheckBox.isChecked()){
			ScriptBlocksApplication.getInstance().getLevelProperties().setLockMasterToGrid(this.lockToGridCheckBox.isChecked());			
		}
		
			ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockX(this.moveBlockXCheckBox.isChecked());					
			ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockY(this.moveBlockYCheckBox.isChecked());					
			ScriptBlocksApplication.getInstance().getLevelProperties().setMoveBlockZ(this.moveBlockZCheckBox.isChecked());			
		
		if(ScriptBlocksApplication.getInstance().getLevelProperties().isShowStats()!=this.showStatsCheckBox.isChecked()){
			ScriptBlocksApplication.getInstance().getLevelProperties().setShowStats(this.showStatsCheckBox.isChecked());
			if(this.showStatsCheckBox.isChecked()){
				ScriptBlocksApplication.getInstance().addStats();
			}else {
				ScriptBlocksApplication.getInstance().removeStats();
			}
		}
		if(ScriptBlocksApplication.getInstance().getLevelProperties().getNextLevelFileName()!=this.textField.getText()){
			ScriptBlocksApplication.getInstance().getLevelProperties().setNextLevelFileName(this.textField.getText());			
		}
		nifty.fromXml("Interfaces/niftytest.xml", "start");
	}
	
}


